<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>海南空管局项目管理平台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/process/demo.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/process/chart.css">
</head>
<style>
    /* 底部固定区域 */
    .layui-layout-admin .layui-footer {
        padding: 10px 0;
        text-align: center;
    }

    .layui-layout-admin .layui-footer {
        background-color: #fff;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-tab-content">
                <div class="layui-form">
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">流程名</label>
                                <div class="layui-input-block">
                                    <input type="text" name="processName" lay-verify="processName"
                                           id="processName"
                                           autocomplete="off" placeholder="请输入流程名"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-col-xs1">
                                <div class="layui-form-mid layui-word-aux"
                                     style="color: red !important;padding: 10px !important;">*
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">流程类别</label>
                                <div class="layui-input-block">
                                    <select id="types">
                                        <option value="">请选流程类别</option>
                                        <option value="one">采购流程</option>
                                        <option value="two">领用流程</option>
                                        <option value="three">出库流程</option>
                                        <option value="four">入库流程</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-xs1">
                                <div class="layui-form-mid layui-word-aux"
                                     style="color: red !important;padding: 10px !important;">*
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">描述</label>
                                <div class="layui-input-block">
                                            <textarea class="layui-textarea" name="processRemarks" id="processRemarks"
                                                      placeholder="请输入描述"></textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-md12">
                    <div class="layui-form">
                        <div class="layui-form-item">
                            <div class="layui-col-xs6">
                                <div class="layui-col-xs6">
                                <div class="layui-inline" style="margin-bottom: 0px;margin-right: 0px;width: 83%">
                                    <label class="layui-form-label">部门</label>
                                    <div class="layui-input-block">
                                        <select id="departmenttid">
                                            <option value="">请选择</option>
                                            <option value="某部门">某部门</option>
                                        </select>
                                    </div>
                                </div>
                                </div>
                                <div class="layui-col-xs6">
                                <div class="layui-inline" style="margin-bottom: 0px;margin-right: 0px;width: 83%">
                                    <label class="layui-form-label">职位</label>
                                    <div class="layui-input-block">
                                        <select id="positiontid">
                                            <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                </div>
                            </div>
                            <div class="layui-inline" style="margin-bottom: 0px;margin-right: 0px;">
                                <div class="layui-input-inline">
                                    <button class="layui-btn" id="change" data-type="reload">添加流程步骤</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-md12">
                    <div id="demo-chart">
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-layout-admin">
                <div class="layui-footer" style="left: 0;padding: 0px;">
                    <button class="layui-btn" lay-submit lay-filter="*" id="pushButton">立即提交</button>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="jsonOutput">
<input type="hidden" value="" id="processId">
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/lib/process/jsPlumb.min.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/lib/process/chart.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script>
    layui.use(['table', 'form'], function () {
        var $ = layui.$
            , table = layui.table
            , layer = layui.layer,
            dtree = layui.dtree,
            form = layui.form;

        $.ajax({
            type: "post",
            url: "<%=request.getContextPath()%>/rest/system/getDepartmentLists",
            dataType: "json",
            async: false,
            success: function (msg) {
                var select = document.getElementById("departmenttid");
                $.each(msg.data, function (i, n) {
                    select.options.add(new Option(n.departmentname, n.departmentname));
                })
                form.render('select');
                return false;
            }
        });

        $.ajax({
            type: "post",
            url: "<%=request.getContextPath()%>/rest/system/getListPosition",
            dataType: "json",
            async: false,
            success: function (msg) {
                var select = document.getElementById("positiontid");
                $.each(msg.data, function (i, n) {
                    select.options.add(new Option(n.positionname, n.positionname));
                })
                form.render('select');
                return false;
            }
        });


        const basicX = 150;
        const startY = 20;
        const endY = 350;
        const newX = 50;
        const newY = 50;


        chart = new Chart($('#demo-chart'));
//        chart = _createChart();
//        var _createChart = function () {
//            return new Chart($('#demo-chart'), {})
//        };
        var nodeStart = chart.addNode('开始', basicX, startY, {
            class: 'node-start',
            removable: false,
            data: {
                name: '开始',
                nodeType: 0
            }
        });
        nodeStart.addPort({
            isSource: true
        });

        //添加结束节点
        var nodeEnd = chart.addNode('结束', basicX, endY, {
            class: 'node-end',
            removable: false,
            data: {
                name: '结束',
                nodeType: 0
            }
        });
        nodeEnd.addPort({
            isTarget: true,
            position: 'Top'
        });


        var addNewTask = function (name, params) {
            params = params || {};
            params.data = params.data || {};
            params.class = 'node-process';
            params.data.nodeType = 1; // 流程节点类型
            var node = chart.addNode(name, newX, newY, params);
            node.addPort({
                isSource: true
            });
            node.addPort({
                isTarget: true,
                position: 'Top'
            });
        };

        $("#change").on('click', function (event) {
            var positiontid = $("#positiontid").val()
            var departmenttid = $("#departmenttid").val()

            if ('' == departmenttid) {
                var departmenttidcheck = document.getElementById("departmenttid");
                departmenttidcheck.setAttribute("class", "layui-form-danger");
                departmenttidcheck.focus();
                layer.msg("请选择部门", {icon: 5, shift: 0});
                return
            }

            if ('' == positiontid) {
                var positiontidcheck = document.getElementById("positiontid");
                positiontidcheck.setAttribute("class", "layui-form-danger");
                positiontidcheck.focus();
                layer.msg("请选择职位", {icon: 5, shift: 0});
                return
            }

            $(this).data('node', $.extend({}, {
                name: departmenttid + '-' + positiontid,
                procId: '0',
                // username: username,
                desc: departmenttid + '-' + positiontid
            }));

            var target = $(event.target);
            var node = target.data('node');
            addNewTask(node.name, {
                data: node
            });
        });

        $('#pushButton').on('click', function () { //搜索的id
            $('#jsonOutput').val(JSON.stringify(chart.toJson()));
            var detail = $("#jsonOutput").val()
            var processName = $("#processName").val();
            var processRemarks = $("#processRemarks").val();
            var types = $("#types").val();

            if (processName == "") {
                var processNameTid = document.getElementById("processName");
                processNameTid.setAttribute("class", "layui-input layui-form-danger");
                processNameTid.focus();
                layer.msg("请输入流程名", {icon: 5, shift: 0});
                return false;
            }
            if (types == "") {
                var typescheck = document.getElementById("types");
                typescheck.setAttribute("class", "layui-form-danger");
                typescheck.focus();
                layer.msg("请选择流程类别", {icon: 5, shift: 0});
                return false;
            }

            var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.6});
            $("#pushButton").addClass("layui-btn layui-btn-disabled").prop("disabled", true);
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/process/insertProcessAll",
                data: "processPackage=" + detail + "&processName=" + processName + "&processRemarks=" + processRemarks + "&types=" + types,
                dataType: "json",
                async: false,
                success: function (obj) {
                    setTimeout(function () {
                        layer.close(index);
                        if (obj = true) {
                            layer.msg('添加成功');
                        } else {
                            layer.msg(obj.errorMsg);
                        }
                    }, 2000);
                    setTimeout(function () {
                        var indexs = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                        parent.layer.close(indexs);//关闭弹出层
//                        window.parent.location.reload();//刷新父页面
                    }, 4000);
                }
            })
        });

    });
</script>
</body>
</html>
